﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.DBUtility;
using StoragePeriod.Model.RawDict;
using StoragePeriod.IDAL.RawDict;

namespace StoragePeriod.SqlServerDAL.RawDict
{
    /// <summary>
    /// 原料操作
    /// </summary>
    public class D_RawMaterial : I_RawMaterial
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RawMaterial");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 根据查询条件获取原料数据列表
        /// </summary>
        /// <param name="eRawMaterial">查询条件</param>
        /// <returns>返回符合要求的原料数据列表</returns>
        public List<E_RawMaterial> GetRawListByWhere(E_RawMaterial eRawMaterial)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from RawMaterial ");
            if (eRawMaterial.StrWhere != "")
            { 
                sbSql.Append(" where "+eRawMaterial.StrWhere);
            }
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_RawMaterial> EntityList = new ModelHandler<E_RawMaterial>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(E_RawMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RawMaterial(");
            strSql.Append("id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2,RAWZS,SafetyTips,PeriodTypeID,idkey,PeriodTypeStorageID,PeriodTypeKnowledgeID,PeriodTypeStorKnowledgeID,UsefulPart,IsEdit)");
            strSql.Append(" values (");
            strSql.Append("@id,@RAWName,@RAWTID,@RAWID,@RAWIDS,@RAWSid,@RAWWid,@InDBid,@Back,@temp1,@temp2,@RAWZS,@SafetyTips,@PeriodTypeID,@idkey,@PeriodTypeStorageID,@PeriodTypeKnowledgeID,@PeriodTypeStorKnowledgeID,@UsefulPart,@IsEdit)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@RAWSid", SqlDbType.VarChar,50),
					new SqlParameter("@RAWWid", SqlDbType.VarChar,50),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@RAWZS", SqlDbType.Int,4),
					new SqlParameter("@SafetyTips", SqlDbType.Text),
					new SqlParameter("@PeriodTypeID", SqlDbType.Int,4),
					new SqlParameter("@idkey", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PeriodTypeStorageID", SqlDbType.Int,4),
					new SqlParameter("@PeriodTypeKnowledgeID", SqlDbType.Int,4),
					new SqlParameter("@PeriodTypeStorKnowledgeID", SqlDbType.Int,4),
					new SqlParameter("@UsefulPart", SqlDbType.Decimal,9),
					new SqlParameter("@IsEdit", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.RAWName;
            parameters[2].Value = model.RAWTID;
            parameters[3].Value = model.RAWID;
            parameters[4].Value = model.RAWIDS;
            parameters[5].Value = model.RAWSid;
            parameters[6].Value = model.RAWWid;
            parameters[7].Value = model.InDBid;
            parameters[8].Value = model.Back;
            parameters[9].Value = model.temp1;
            parameters[10].Value = model.temp2;
            parameters[11].Value = model.RAWZS;
            parameters[12].Value = model.SafetyTips;
            parameters[13].Value = model.PeriodTypeID;
            parameters[14].Value = model.idkey;
            parameters[15].Value = model.PeriodTypeStorageID;
            parameters[16].Value = model.PeriodTypeKnowledgeID;
            parameters[17].Value = model.PeriodTypeStorKnowledgeID;
            parameters[18].Value = model.UsefulPart;
            parameters[19].Value = model.IsEdit;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_RawMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RawMaterial set ");
            strSql.Append("RAWName=@RAWName,");
            strSql.Append("RAWTID=@RAWTID,");
            strSql.Append("RAWID=@RAWID,");
            strSql.Append("RAWIDS=@RAWIDS,");
            strSql.Append("RAWSid=@RAWSid,");
            strSql.Append("RAWWid=@RAWWid,");
            strSql.Append("InDBid=@InDBid,");
            strSql.Append("Back=@Back,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2,");
            strSql.Append("RAWZS=@RAWZS,");
            strSql.Append("SafetyTips=@SafetyTips,");
            strSql.Append("PeriodTypeID=@PeriodTypeID,");
            strSql.Append("idkey=@idkey,");
            strSql.Append("PeriodTypeStorageID=@PeriodTypeStorageID,");
            strSql.Append("PeriodTypeKnowledgeID=@PeriodTypeKnowledgeID,");
            strSql.Append("PeriodTypeStorKnowledgeID=@PeriodTypeStorKnowledgeID,");
            strSql.Append("UsefulPart=@UsefulPart,");
            strSql.Append("IsEdit=@IsEdit");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@RAWSid", SqlDbType.VarChar,50),
					new SqlParameter("@RAWWid", SqlDbType.VarChar,50),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@RAWZS", SqlDbType.Int,4),
					new SqlParameter("@SafetyTips", SqlDbType.Text),
					new SqlParameter("@PeriodTypeID", SqlDbType.Int,4),
					new SqlParameter("@idkey", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PeriodTypeStorageID", SqlDbType.Int,4),
					new SqlParameter("@PeriodTypeKnowledgeID", SqlDbType.Int,4),
					new SqlParameter("@PeriodTypeStorKnowledgeID", SqlDbType.Int,4),
					new SqlParameter("@UsefulPart", SqlDbType.Decimal,9),
					new SqlParameter("@IsEdit", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RAWName;
            parameters[1].Value = model.RAWTID;
            parameters[2].Value = model.RAWID;
            parameters[3].Value = model.RAWIDS;
            parameters[4].Value = model.RAWSid;
            parameters[5].Value = model.RAWWid;
            parameters[6].Value = model.InDBid;
            parameters[7].Value = model.Back;
            parameters[8].Value = model.temp1;
            parameters[9].Value = model.temp2;
            parameters[10].Value = model.RAWZS;
            parameters[11].Value = model.SafetyTips;
            parameters[12].Value = model.PeriodTypeID;
            parameters[13].Value = model.idkey;
            parameters[14].Value = model.PeriodTypeStorageID;
            parameters[15].Value = model.PeriodTypeKnowledgeID;
            parameters[16].Value = model.PeriodTypeStorKnowledgeID;
            parameters[17].Value = model.UsefulPart;
            parameters[18].Value = model.IsEdit;
            parameters[19].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterial ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterial ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_RawMaterial GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2,RAWZS,SafetyTips,PeriodTypeID,idkey,PeriodTypeStorageID,PeriodTypeKnowledgeID,PeriodTypeStorKnowledgeID,UsefulPart,IsEdit from RawMaterial ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            E_RawMaterial model = new E_RawMaterial();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWName"] != null)
                {
                    model.RAWName = ds.Tables[0].Rows[0]["RAWName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWTID"].ToString() != "")
                {
                    model.RAWTID = int.Parse(ds.Tables[0].Rows[0]["RAWTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWID"].ToString() != "")
                {
                    model.RAWID = int.Parse(ds.Tables[0].Rows[0]["RAWID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWIDS"].ToString() != "")
                {
                    model.RAWIDS = int.Parse(ds.Tables[0].Rows[0]["RAWIDS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWSid"] != null)
                {
                    model.RAWSid = ds.Tables[0].Rows[0]["RAWSid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWWid"] != null)
                {
                    model.RAWWid = ds.Tables[0].Rows[0]["RAWWid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InDBid"] != null)
                {
                    model.InDBid = ds.Tables[0].Rows[0]["InDBid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null)
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null)
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWZS"].ToString() != "")
                {
                    model.RAWZS = int.Parse(ds.Tables[0].Rows[0]["RAWZS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SafetyTips"] != null)
                {
                    model.SafetyTips = ds.Tables[0].Rows[0]["SafetyTips"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PeriodTypeID"].ToString() != "")
                {
                    model.PeriodTypeID = int.Parse(ds.Tables[0].Rows[0]["PeriodTypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["idkey"].ToString() != "")
                {
                    model.idkey = new Guid(ds.Tables[0].Rows[0]["idkey"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PeriodTypeStorageID"].ToString() != "")
                {
                    model.PeriodTypeStorageID = int.Parse(ds.Tables[0].Rows[0]["PeriodTypeStorageID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PeriodTypeKnowledgeID"].ToString() != "")
                {
                    model.PeriodTypeKnowledgeID = int.Parse(ds.Tables[0].Rows[0]["PeriodTypeKnowledgeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PeriodTypeStorKnowledgeID"].ToString() != "")
                {
                    model.PeriodTypeStorKnowledgeID = int.Parse(ds.Tables[0].Rows[0]["PeriodTypeStorKnowledgeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UsefulPart"].ToString() != "")
                {
                    model.UsefulPart = decimal.Parse(ds.Tables[0].Rows[0]["UsefulPart"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsEdit"].ToString() != "")
                {
                    model.IsEdit = int.Parse(ds.Tables[0].Rows[0]["IsEdit"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2,RAWZS,SafetyTips,PeriodTypeID,idkey,PeriodTypeStorageID,PeriodTypeKnowledgeID,PeriodTypeStorKnowledgeID,UsefulPart,IsEdit ");
            strSql.Append(" FROM RawMaterial ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method
    }
}
